package ExpressOne;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MemoryShow extends JFrame implements ActionListener {
    MyMemory memory;
    JTextField jtf_text;
    JTextArea jta_text;
    JPanel p_up, p_down;
    JButton btn_resign, btn_interrupt, btn_runToBlock, btn_blockToReady, btn_runToReady;
    JButton btn_handOn, btn_handOnToReady;
    Thread thread;


    public MemoryShow() {
        super("内存运行模拟");
        while (!init()) ;

        p_up = new JPanel();
        p_down = new JPanel(new GridLayout(3, 2));

        jta_text = new JTextArea();
        jta_text.setRows(16);
        jta_text.setLineWrap(true);
        jta_text.setEnabled(false);
        jta_text.setFont(new Font("宋体", Font.BOLD, 18));
        jta_text.setDisabledTextColor(Color.black);

        jtf_text = new JTextField();
        jtf_text.setColumns(20);
        jtf_text.setFont(new Font("宋体", Font.BOLD, 18));

        btn_resign = new JButton("申请内存");
        btn_runToBlock = new JButton("发生阻塞事件");
        btn_blockToReady = new JButton("阻塞事件完成");
        btn_runToReady = new JButton("时间片轮转");
        btn_handOn = new JButton("挂起");
        btn_handOnToReady = new JButton("唤醒");
        btn_interrupt = new JButton("终止");

        btn_handOn.addActionListener(this);
        btn_resign.addActionListener(this);
        btn_runToBlock.addActionListener(this);
        btn_interrupt.addActionListener(this);
        btn_blockToReady.addActionListener(this);
        btn_runToReady.addActionListener(this);
        btn_handOnToReady.addActionListener(this);

        p_up.add(jtf_text);
        p_up.add(btn_resign);

        p_down.add(btn_runToBlock);
        p_down.add(btn_blockToReady);
        p_down.add(btn_runToReady);
        p_down.add(btn_handOn);
        p_down.add(btn_handOnToReady);
        p_down.add(btn_interrupt);

        add(p_up, BorderLayout.NORTH);
        add(p_down, BorderLayout.CENTER);
        add(new JScrollPane(jta_text), BorderLayout.SOUTH);

        setResizable(false);
        setBounds(800, 100, 400, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        this.myShow();
    }

    //初始化
    boolean init() {
        //容错处理一
        int maxSize;
        String str1 = JOptionPane.showInputDialog(this, "请输入内存大小", "内存", JOptionPane.INFORMATION_MESSAGE);
        if (str1 == null)
            System.exit(0);
        try {
            maxSize = Integer.parseInt(str1);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "输入错误，请重新输入");
            return false;
        }
        //容错处理二
        int maxDataSize;
        String str2 = JOptionPane.showInputDialog(this, "请输入外存大小", "外存", JOptionPane.INFORMATION_MESSAGE);
        if (str2 == null)
            System.exit(0);
        try {
            maxDataSize = Integer.parseInt(str2);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "输入错误，请重新输入");
            return false;
        }

        memory = new MyMemory(maxSize, maxDataSize);
        return true;
    }

    public void myShow() {
        thread = new Thread(() -> jta_text.setText(memory.showMessage() + "\n\n" + jta_text.getText()));
        thread.start();
        memory.showColor();
    }

    //主函数
    public static void main(String[] args) {
        new MemoryShow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "申请内存":
                String str = jtf_text.getText();
                try {
                    int size = Integer.parseInt(str);
                    if (!memory.addToReady(size)) {
                        JOptionPane.showMessageDialog(this, "没有内存", "内存不足", JOptionPane.INFORMATION_MESSAGE);
                    }
                    memory.readyToRunning();
                } catch (Exception ee) {
                    JOptionPane.showMessageDialog(this, "非法输入", "错误", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "发生阻塞事件":
                if (!memory.runningToBlock()) {
                JOptionPane.showMessageDialog(this, "非法操作", "错误", JOptionPane.ERROR_MESSAGE);
            }
            break;
            case "阻塞事件完成":
                if (!memory.blockToReady()) {
                    JOptionPane.showMessageDialog(this, "非法操作", "错误", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "时间片轮转":
                if (!memory.runningToReady()) {
                    JOptionPane.showMessageDialog(this, "非法操作", "错误", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "挂起":
                if (!memory.blockToHandOn()) {
                    JOptionPane.showMessageDialog(this, "外存不足/非法操作", "错误", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "唤醒":
                if (!memory.handOnToReady()) {
                    JOptionPane.showMessageDialog(this, "内存不足/没有需要唤醒的程序", "错误", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "终止":
                memory.interrupt();
                break;
        }
        this.myShow();
    }
}